# Simple Makefile
#
#  Author: Jesper Dangaard Brouer <brouer@redhat.com>
#
SRCS =  ipv6_example01.c udp_example02.c udp_echo.c udp_client_echo.c \
	qdisc_bypass_test.c udp_flood.c udp_sink.c \
	tcp_sink.c tcp_sink_client.c \
	tcp_sink_epoll.c \
	array_compare01.c \
	burn_cpu.c udp_snd.c \
	syscall_overhead.c \
	get_nic_driver.c \
	udp_pacer.c

# From kernel src: scripts/subarch.include
ARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
                               -e s/sun4u/sparc64/ \
                               -e s/arm.*/arm/ -e s/sa110/arm/ \
                               -e s/s390x/s390/ -e s/parisc64/parisc/ \
                               -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
                               -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
                               -e s/riscv.*/riscv/)

# Some program a limited to a specific CPU architecture
ifeq ($(ARCH),x86)
SRCS += overhead_cmpxchg.c
endif

OBJECTS = common_socket.o common.o
HEADERS = ${OBJECTS:.o=.h}

TARGETS = ${SRCS:.c=} compiler_test01

# librt needed for 'clock_gettime'
LIBS=-lrt -lpthread
LIBS_PCAP=-lpcap

CFLAGS := -O2 -Wall -g

all: $(TARGETS)

help:
	@echo Possible targets: $(TARGETS)

# Create dependency to OBJECTS
$(TARGETS): $(OBJECTS)

# OBJECTS
%.o: %.c $(HEADERS) global.h Makefile
	$(CC) $(CFLAGS) $(OLEVEL) -c  $< -o $@

# TARGETS
.c: $<
	gcc $(CFLAGS) $(LIBS) -o $@ $< $(OBJECTS)

pcap_timeread: pcap_timeread.c
	gcc -o $@ $(LIBS_PCAP) $<

pcap_read: pcap_read.c
	gcc -o $@ $(LIBS_PCAP) $<

pcap_dump: pcap_dump.c
	gcc -o $@ $(LIBS_PCAP) $<

pcap_write_sample: pcap_write_sample.c
	gcc -o $@ $(LIBS_PCAP) $<

# Special compiler option to trigger issue
compiler_test01: compiler_test01.c
	gcc -o $@ $(CFLAGS) -fno-strict-overflow $<

cscope:
	@echo "Generate Cscope files"
	@find . -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" > cscope.files
	@cscope -q -R -b -i cscope.files

clean:
	@echo "Removing autogenerated files."
	@find . -type f \( -name '*~' -o -name '*.o' -o -name 'core' \) \
	        -print | xargs rm -f
	@echo "Removing binary targets"
	rm -f $(TARGETS)
